package com.health.report_form;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.result.ResultUtils;
import com.health.result.ResultVo;
import com.health.security.Admin;
import com.health.security.CentralAuthenticationToken;
import com.health.report.ReportForm;
import com.health.security.ReportFormService;
import com.health.security.Student;
import com.health.vo.ParmVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: zipeng Li
 * 2021/6/20  16:53
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
public class reportController {
    @Resource
    private ReportFormService reportFormService;

    /**
     * 提交表单
     * @param reportForm
     * @return
     */
    @PostMapping("report-form")
    public ResultVo reportForm(@RequestBody ReportForm reportForm){
        // 判断当前用户是否已经提交
        CentralAuthenticationToken authentication = (CentralAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        Student student = authentication.getStudent();

        ReportForm one = reportFormService.getDayRecord(student.getId());
        if (one != null) return ResultUtils.error("当日已经打卡，勿重复提交");

        // 保存数据库
        reportForm.setCentralAuthenticationId(student.getId());
        reportForm.setName(student.getName());
        reportFormService.save(reportForm);
        return ResultUtils.success("打卡成功");
    }

    /**
     * 判断今天是否提交成功
     * @return
     */
    @PostMapping("check-report")
    public ResultVo checkReport(){
        // 判断当前用户是否已经提交
        CentralAuthenticationToken authentication = (CentralAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        Student student = authentication.getStudent();
        ReportForm one = reportFormService.getDayRecord(student.getId());
        if (one != null) return ResultUtils.success("您今天已经提交成功啦", true);
        return ResultUtils.success("您今天还没提交哦", false);
    }

}
